import { createRouter, createWebHistory, Router } from 'vue-router'
import { storage } from '@/store/sessionStorage'

const routes: any[] = [
    {
        path: '/',
        name: 'home',
        redirect: 'index'
    },
    {
        path: '/index',
        name: 'index',
        component: () => import('@/pages/home/Index.vue'),
        meta: {
            title: '车况查询',
            permission: ['*']
        }
    },
    {
        path: '/:pathMatch(.*)',
        //访问主页的时候 重定向到index页面
        redirect: '/404'
    },
    {
        path: '/404',
        name: '/404',
        component: () => import('@/pages/404/Index.vue'),
        meta: {
            title: '',
            permission: ['*']
        }
    },
    {
        path: '/protocol/:type',
        name: 'protocol',
        component: () => import('@/pages/protocol/Index.vue'),
        meta: {
            title: '',
            permission: ['*']
        }
    },
    {
        path: '/no-permission',
        name: 'no-permission',
        component: () => import('@/pages/noPermission/Index.vue'),
        meta: {
            title: '无权访问',
            permission: ['*']
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/pages/login/login/Index.vue'),
        meta: {
            title: '车况查询',
            permission: ['*']
        }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/pages/login/register/Index.vue'),
        meta: {
            title: '注册',
            keepAlive: true,
            permission: ['*']
        }
    },
    {
        path: '/retrieve',
        name: 'retrieve',
        component: () => import('@/pages/login/retrieve/Index.vue'),
        meta: {
            title: '找回密码',
            permission: ['*']
        }
    },
    {
        path: '/personal-center',
        name: 'personal-center',
        component: () => import('@/pages/personalCenter/Index.vue'),
        meta: {
            title: '个人中心',
            permission: ['*']
        }
    },
    {
        path: '/message/list',
        name: 'message-list',
        component: () => import('@/pages/personalCenter/message/list/Index.vue'),
        meta: {
            title: '消息列表',
            permission: ['*']
        }
    },
    {
        path: '/message/:id',
        name: 'message-detailed',
        component: () => import('@/pages/personalCenter/message/detailed/Index.vue'),
        meta: {
            title: '消息详情',
            permission: ['*']
        }
    },
    {
        path: '/wallet',
        name: 'wallet',
        component: () => import('@/pages/personalCenter/wallet/pay/Index.vue'),
        meta: {
            title: '我的钱包',
            permission: ['*']
        }
    },
    {
        path: '/wallet/record',
        name: 'wallet-record',
        component: () => import('@/pages/personalCenter/wallet/record/Index.vue'),
        meta: {
            title: '消费记录',
            permission: ['*']
        }
    },
    {
        path: '/vehicle-condition/detection',
        name: 'vehicle-condition-detection',
        component: () => import('@/pages/vehicleCondition/detection/Index.vue'),
        meta: {
            title: '',
            permission: ['*']
        }
    },
    {
        path: '/vehicle-condition/query',
        name: 'vehicle-condition-query',
        component: () => import('@/pages/vehicleCondition/query/Index.vue'),
        meta: {
            title: '报告查询中',
            permission: ['*']
        }
    },
    {
        path: '/report-html',
        name: 'report-html',
        component: () => import('@/pages/report/html/Index.vue'),
        meta: {
            title: '出险车况报告',
            permission: ['*']
        }
    },
    {
        path: '/collision',
        name: 'collision',
        component: () => import('@/pages/report/collision/Index.vue'),
        meta: {
            title: '出险车况报告',
            permission: ['*']
        }
    },
    {
        path: '/new-energy',
        name: 'new-energy',
        component: () => import('@/pages/report/newEnergy/Index.vue'),
        meta: {
            title: '新能源车况报告',
            permission: ['*']
        }
    },
    {
        path: '/webview',
        name: 'webview',
        component: () => import('@/pages/webview/Index.vue'),
        meta: {
            title: '维保车况报告',
            permission: ['*']
        }
    },
    {
        path: '/vehicle-condition/case',
        name: 'vehicle-condition-case',
        component: () => import('@/pages/vehicleCondition/case/Index.vue'),
        meta: {
            title: '案例',
            permission: ['*']
        }
    },
    {
        path: '/vehicle-condition/query-record',
        name: 'vehicle-condition-query-record',
        component: () => import('@/pages/vehicleCondition/queryRecord/Index.vue'),
        meta: {
            title: '查询记录',
            permission: ['*']
        }
    },
    {
        path: '/invoice',
        name: 'invoice',
        component: () => import('@/pages/personalCenter/invoice/index/Index.vue'),
        meta: {
            title: '开具发票',
            permission: ['*']
        }
    },
    {
        path: '/invoice/make-out',
        name: 'invoice-make-out',
        component: () => import('@/pages/personalCenter/invoice/makeOut/Index.vue'),
        meta: {
            title: '开具发票',
            permission: ['*']
        }
    },
    {
        path: '/invoice/submit',
        name: 'invoice-submit',
        component: () => import('@/pages/personalCenter/invoice/submit/Index.vue'),
        meta: {
            title: '开具发票',
            permission: ['*']
        }
    },
    {
        path: '/invoice/history',
        name: 'invoice-history',
        component: () => import('@/pages/personalCenter/invoice/history/Index.vue'),
        meta: {
            title: '发票记录',
            permission: ['*']
        }
    },
    {
        path: '/invoice/:id',
        name: 'invoice-detailed',
        component: () => import('@/pages/personalCenter/invoice/detailed/Index.vue'),
        meta: {
            title: '发票详情',
            permission: ['*']
        }
    },
    {
        path: '/invoice/order',
        name: 'invoice-order',
        component: () => import('@/pages/personalCenter/invoice/order/Index.vue'),
        meta: {
            title: '查看发票订单',
            permission: ['*']
        }
    },
    {
        path: '/refund/make-out',
        name: 'refund-make-out',
        component: () => import('@/pages/personalCenter/wallet/refund/makeOut/Index.vue'),
        meta: {
            title: '申请退款',
            permission: ['*']
        }
    },
    {
        path: '/refund/submit',
        name: 'refund-submit',
        component: () => import('@/pages/personalCenter/wallet/refund/submit/Index.vue'),
        meta: {
            title: '申请退款',
            permission: ['*']
        }
    },
    {
        path: '/refund/:id',
        name: 'refund-detailed',
        component: () => import('@/pages/personalCenter/wallet/refund/detailed/Index.vue'),
        meta: {
            title: '退款详情',
            permission: ['*']
        }
    },
    {
        path: '/refund/history',
        name: 'refund-list',
        component: () => import('@/pages/personalCenter/wallet/refund/history/Index.vue'),
        meta: {
            title: '退款记录',
            permission: ['*']
        }
    }
]
const router: Router = createRouter({
    history: createWebHistory(),
    routes
})
//路由权限判断
router.beforeEach((to, from, next) => {
    if (storage.get('token') === '') {
        //以下路由页面不需要登录也可访问
        if (
            [
                'login',
                'register',
                'retrieve',
                'protocol',
                'index',
                'webview',
                'message-list',
                'message-detailed',
                'collision',
                'new-energy'
            ].includes(to.name as string)
        ) {
            next()
        } else {
            //返回到首页
            next({ name: 'index' })
        }
    } else {
        //根据路由中的权限配置，判断当前页面是否有权访问
        if ((to.meta.permission as string[]).includes('admin') && storage.get('isAdmin') == 'false') {
            next({ name: 'no-permission' })
        } else {
            next()
        }
    }
    if (to.meta.title) {
        // 判断是否有标题
        document.title = to.meta.title as string
        console.log('标题', to.meta.title, JSON.parse(storage.get('isAdmin')), storage.get('isAdmin'))
    }
})

export default router
